﻿
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports wfileis.data

Public Class catView
    Inherits cacheControlProcessor

    Public Const _pathidentity As String = "/cat"
    Public Const searchCatClassName As String = "sc"
    Public Const ignoreCatClassName As String = "ic"
    Public Const resultClassName As String = "r"

    Protected Overrides Function normalretiredticks() As Long
        Return secondsToTicks(30)
    End Function

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Shared Function baseurl() As String
        Return strmid(_pathidentity, strlen(network.pathSeperator))
    End Function

    Public Shared Function generateCatViewlink(ByVal context As httpContext, ByVal cat As String, _
                                               Optional ByVal ignoreText As Boolean = True) As String
        assert(Not context Is Nothing, "context is nothing.")

        Dim rtn As String = Nothing
        rtn = generateLink(context, baseurl(), query.category, ignoreText)
        rtn = urlUtils.appendQuery(rtn, query.category, cat)

        Return rtn
    End Function

    Public Shared Function generateCatViewlink(ByVal cat As String, _
                                               Optional ByVal ignoreText As Boolean = True) As String
        Return generateCatViewlink(httpServer2.currentContext(), cat, ignoreText)
    End Function

    Private Function processNoCat(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        assert(Not context Is Nothing, "context is nothing.")
        assert(control Is Nothing, "control should be nothing.")

        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage())
        page.title().text = context.string(stringId.CATVIEW_TITLE_NOCAT)

        page.body().addchild(header.generateInstance(page))

        Dim label As label = Nothing
        label = context.createControl(New label())
        label.text = context.string(stringId.CATVIEW_NOCAT)
        page.body().addchild(label)
        page.body().addchild(New br())

        Dim a As a = Nothing
        a = context.createControl(New a())
        a.text = context.string(stringId.AUTOGOBACKTOHOMEPAGE)
        a.href = network.thislevelPathMark
        page.body().addchild(a)

        Dim delayjump As delayJump = Nothing
        delayjump = context.createControl(New delayJump())
        delayjump.destination = a.href
        delayjump.delay = 10
        page.head().addchild(delayjump)

        control = page
        Return True
    End Function

    Private Function processCatNoFound(ByVal context As httpContext, ByRef control As xmlcontrol, _
                                       ByVal cat As String) As Boolean
        assert(Not context Is Nothing, "context is nothing.")
        assert(control Is Nothing, "control should be nothing.")

        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage())
        page.title().text = context.string(stringId.CATVIEW_TITLE_CATNOFOUND)

        page.body().addchild(header.generateInstance(page))

        Dim label As label = Nothing
        label = context.createControl(New label())
        label.text = context.string(stringId.CATVIEW_CATNOFOUND) + character.blank + cat
        page.body().addchild(label)
        page.body().addchild(context.createControl(New br()))

        addwriter(context, page, page.body(), True)

#If RETIRED Then
        Dim a As a = Nothing
        a = context.createControl(New a())
        a.text = context.string(stringId.VIEW_WIRTESOMETHINGTOKEY)
        a.href = urlUtils.appendQuery(homepage._pathidentity, homepage.keyQuery, key)
        page.body().addchild(a)
#End If

        control = page
        Return True
    End Function

    Private Function processCat(ByVal context As httpContext, ByRef control As xmlcontrol, _
                                ByVal cat As String, ByVal category As category, _
                                ByVal ignoreCats As vector(Of String)) As Boolean
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not category Is Nothing, "category is nothing.")
        assert(control Is Nothing, "control should be nothing.")

        Dim page As xhtmlpage = Nothing
        page = context.createControl(New xhtmlpage())
        page.addcsslink(cssrequest.generateCssfileLinkUrl("catview.css"))
        page.title().text = cat + context.string(stringId.CATVIEW_TITLE_RESULT)

        page.body().addchild(header.generateInstance(page))

        Dim div As div = Nothing
        div = context.createControl(New div())
        div.class = searchCatClassName
        page.body().addchild(div)

        Dim span As span = Nothing
        span = context.createControl(New span())
        span.text = context.string(stringId.CATVIEW_SEARCHCATS)
        div.addchild(span)

        span = context.createControl(New span())
        span.text = cat
        div.addchild(span)

        If Not ignoreCats Is Nothing AndAlso ignoreCats.size() > 0 Then
            div = context.createControl(New div())
            div.class = ignoreCatClassName
            page.body().addchild(div)

            span = context.createControl(New span())
            span.text = context.string(stringId.CATVIEW_IGNORECATS)
            div.addchild(span)

            span = context.createControl(New span())
            span.text = ignoreCats.ToString(character.blank)
            div.addchild(span)
        End If

        Dim results As div = Nothing
        results = context.createControl(New div())
        results.class = resultClassName
        page.body().addchild(results)

        div = context.createControl(New div())
        div.text = context.string(stringId.CATVIEW_RESULTAREA)
        results.addchild(div)

        Dim s As [set](Of String) = Nothing
        s = New [set](Of String)()
        Dim it As category.iterator = Nothing
        it = category.rbegin()
        While it <> category.rend()
            If s.find(+it) = s.end() Then
                s.insert(+it)
                div = context.createControl(New div())
                results.addchild(div)

                Dim a As a = Nothing
                a = context.createControl(New a())
                a.text = +it
                a.href = view.generateViewlink(context, (+it))
                div.addchild(a)
            End If

            it -= 1
        End While

        addwriter(context, page, page.body(), True)

        control = page
        Return True
    End Function

    Private Function readcategories(ByVal catstr As String, ByRef cat As category, _
                                    ByRef ignoreCats As vector(Of String)) As Boolean
        Dim cats As vector(Of String) = Nothing
        separateCategories(catstr, cats)
        If cats Is Nothing OrElse cats.size() = 0 Then
            Return False
        Else
            Dim categories As vector(Of [set](Of String)) = Nothing
            categories = New vector(Of [set](Of String))()
            If ignoreCats Is Nothing Then
                ignoreCats = New vector(Of String)()
            Else
                ignoreCats.clear()
            End If

            Dim cur As category = Nothing
            cur = New category()
            Dim i As Int64
            For i = 0 To cats.size() - 1
                If cur.read(cats(i)) Then
                    categories.push_back(list2set(cur))
                Else
                    ignoreCats.push_back(cats(i))
                End If
            Next

            If categories.size() = 0 Then
                Return False
            End If

            If cat Is Nothing Then
                cat = New category()
            Else
                cat.clear()
            End If

            Dim it As [set](Of String).iterator = Nothing
            it = categories(0).begin()
            While it <> categories(0).end()
                For i = 1 To categories.size() - 1
                    If categories(i).find(+it) = categories(i).end() Then
                        Exit For
                    End If
                Next

                If i = categories.size() Then
                    cat.push_back(+it)
                End If
                it += 1
            End While

            Return cat.size() > 0
        End If
    End Function

    Protected Overrides Function processImpl(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        assert(Not context Is Nothing, "context is nothing.")
        Dim cats As String = Nothing
        cats = context.url().query(query.category)
        If isemptystring(cats) Then
            Return processNoCat(context, control)
        Else
            Dim category As category = Nothing
            Dim ignoreCats As vector(Of String) = Nothing
            If Not readcategories(cats, category, ignoreCats) Then
                Return processCatNoFound(context, control, cats)
            Else
                Return processCat(context, control, cats, category, ignoreCats)
            End If
        End If

        debugassert(False, "should cover all possibilities already.")
        Return False
    End Function
End Class
